<?php
/**
 * Geneone: Content Management Framework
 * Forum Report
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Object_Forum_Report {
	var $object = null;
	
	function Gene_Object_Forum_Report(&$object) {
		$this->object =& $object;
	}
	
	/**
	 * Lists all reports on this comment.
	 *
	 * @return array All reports
	 */
	function getAll() {
		$db =& Gene::getDatabase();
		$info = $this->object->get();
		
		$q = $db->prepare ("SELECT r.*, u.name FROM ".Gene::tableName("forum_reports")." r LEFT JOIN ".Gene::tableName("user_info")." u ON (r.user_id=u.user_id) WHERE r.object_id=? ORDER BY r.report_time DESC");
		$res = $q->execute(array($info['object_id']));
		return $res->fetchAll();
	}
	
	/**
	 * Returns this user's report on this comment.
	 *
	 * @var integer $userid Optionally provide the user ID of reporter
	 *
	 * @return mixed array report if report exists, otherwise false.
	 */
	function get($userid=null) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		$info = $this->object->get();
		
		if (!$userid) {
			$userid = $user->getUserId();
		}
		$userid = (int)$userid;
		
		if (!$userid) {
			return false;
		}
		
		$q = $db->prepare ("SELECT * FROM ".Gene::tableName("forum_reports")." WHERE user_id=? AND object_id=?");
		$res = $q->execute(array($userid, $info['object_id']));
		
		if ($row = $res->fetchRow()) {
			$res->free();
			return $row;
		} else {
			return false;
		}
	}
	
	/**
	 * Reports the comment to a moderator.
	 *
	 * If a report for this user already exists, the old one is replaced.
	 *
	 * @var string $type Type of complaint:
	 *                   1 - Abusive
	 *                   2 - Offensive or Inappropiate
	 *                   3 - Language
	 *                   4 - Spam
	 *                   5 - Off Topic
	 *                   6 - Other
	 * @var string $comments Additional Comments
	 * @var integer $userid Optionally provide the user ID of reporter
	 *
	 * @return bool Status
	 */
	function set($type, $comments='', $userid=null) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		$info = $this->object->get();
		
		if (!$userid) {
			$userid = $user->getUserId();
		}
		$userid = (int)$userid;
		
		if (!$userid) {
			return false;
		}
		
		$type = (int)$type;
		if ($type < 1 || $type > 6) {
			return false;
		}
		
		if ($this->get()) {
			$db->extended->autoExecute(
				Gene::tableName("forum_reports"),
				array(
					"report_type" => $type,
					"report_time" => time(),
					"comments" => $comments ? $comments : null,
				),
				MDB2_AUTOQUERY_UPDATE,
				"user_id='".$userid."' AND object_id='".$info['object_id']."'"
			);
		} else {
			$p = array_reverse($this->object->getParents());
			$forumid = 0;
			foreach ($p as $i) {
				if ($i['type'] == "forum") {
					$forumid = $i['object_id'];
					continue;
				}
			}
			
			$db->extended->autoExecute(
				Gene::tableName("forum_reports"),
				array(
					"user_id" => $userid,
					"object_id" => $info['object_id'],
					"forum_id" => $forumid,
					"discussion_id" => $info['parent'],
					"report_type" => $type,
					"report_time" => time(),
					"comments" => $comments ? $comments : null,
				),
				MDB2_AUTOQUERY_INSERT
			);
		}
		return true;
	}
	
	/**
	 * Removes this user's report on this comment.
	 *
	 * @var integer $userid Optionally provide the user ID of reporter
	 */
	function remove($userid=null) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		$info = $this->object->get();
		
		if (!$userid) {
			$userid = $user->getUserId();
		}
		$userid = (int)$userid;
		
		if (!$userid) {
			return false;
		}
		
		$q = $db->prepare ("DELETE FROM ".Gene::tableName("forum_reports")." WHERE user_id=? AND object_id=?");
		$q->execute(array($userid, $info['object_id']));
	}
	
	/**
	 * Remove all reports on this comment.
	 */
	function removeAll() {
		$db =& Gene::getDatabase();
		$info = $this->object->get();
		
		$q = $db->prepare ("DELETE FROM ".Gene::tableName("forum_reports")." WHERE object_id=?");
		$q->execute(array($info['object_id']));
	}
}
?>